import pandas as pd
import numpy as np
import pyecharts.options as opts
from pyecharts.charts import Boxplot
# from pyecharts.globals import ThemeType

class Boxplot_flow_years():
    
    def __init__(self, dataset, place=0):
        self.paint_data=dataset
        self.place=place #九寨沟——0；四姑娘山——1
        
        #主程序入口
        self.main()
    
    #主程序入口函数实现
    def main(self):
        
        # 调用获取数据函数
        self.getData()
        # 调用绘图函数
        self.paint()
        
        
    def getData(self):
        
        df=self.paint_data.loc[:,['date','flow']] #提取日期和客流数据
        df.columns = ['date','flow'] #表头名称
        df['date'] = pd.to_datetime(df['date']) #将数据类型转换为日期类型
        df = df.set_index('date') # 将date设置为index
        self.paint_data=df
        # print(self.paint_data)
        
        
        # a=type(self.paint_data.index[0])
        # print(self.paint_data.index[0])
        # print(a)
    
    
    #绘制箱线图
    def paint(self):
        
        if self.place==0:
            years=range(2013, 2017) #设置年份范围
            title='九寨沟2013-2016年进沟客流箱线图'
        else:
            years=range(2016, 2020) #设置年份范围
            title="四姑娘山2016-2019年旅游客流箱线图"
        
        fileName='3.2 '+title+".html"
        
        y_data=[]
        xlabel=[]
        # 提取每年的客流量
        for i in years:
            # print(self.paint_data.keys())
            start=str(i)+"-01-01"
            end=str(i)+"-12-31"
            x=self.paint_data[start:end] #筛选第i年的客流数据
            # x=self.paint_data.loc[str(i)] #筛选第i年的客流数据
            x=np.array(x).tolist()
            z=[]
            for y in x:
                z=z+y
            y_data.append(z)
            year=[str(i)+'年']
            xlabel=xlabel+year
        
        box_plot = Boxplot(init_opts=opts.InitOpts(width="900px", height="600px"))
        box_plot = (
            box_plot
            .add_xaxis(xaxis_data=xlabel)
            .add_yaxis(series_name="", y_axis=box_plot.prepare_data(y_data))
            .set_global_opts(
                title_opts=opts.TitleOpts(
                    pos_left="center", title=title
                ),
                tooltip_opts=opts.TooltipOpts(trigger="item", axis_pointer_type="shadow"),
                xaxis_opts=opts.AxisOpts(
                    type_="category",
                    boundary_gap=True,
                    splitarea_opts=opts.SplitAreaOpts(is_show=False),
                    axislabel_opts=opts.LabelOpts(formatter="{value}"),
                    splitline_opts=opts.SplitLineOpts(is_show=False),
                ),
                yaxis_opts=opts.AxisOpts(
                    type_="value",
                    name="日客流量",
                    splitarea_opts=opts.SplitAreaOpts(
                        is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
                    ),
                ),
            )
            .set_series_opts(tooltip_opts=opts.TooltipOpts(formatter="{b}: {c}"))
            .render('resultFile/'+fileName)
        )
        
        print(fileName+"已生成！")

